/**
 * Title:        流程监控数据监测
 * Company:      泛微软件
 */


package checkDetail;

import general.BaseBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;



public class MonitorInfoCheck extends BaseBean implements CheckBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("MonitorInfoCheck  start");

        boolean isoracle = (recordSet.getDBType()).equals("oracle") ;
        List resultList = new ArrayList();
        String result = "";
        String dbtype = recordSet.getDBType();

        String sql= "";
        if (dbtype.equalsIgnoreCase("oracle")) {
            sql = "select 1 from user_indexes where INDEX_NAME = upper('IX_WF_MONITOR_INFO_JJI')";
        } else if (dbtype.toLowerCase().indexOf("sqlserver")>-1||dbtype.equalsIgnoreCase("sybase")) {
            sql = "select 1 from sysobjects where name = 'IX_WF_MONITOR_INFO_JJI' ";
        }
        result = resultOk;
        recordSet.executeSql(sql);
        if(recordSet.next()){
            result = resultNeedFix;
            Map resultMap = new HashMap<String,String>();
            resultMap.put("showMessage","数据库中存在索引IX_WF_MONITOR_INFO_JJI，请修复");
            resultMap.put("buttonname","修复");
            resultList.add(resultMap);
        }
        
        recordSet.executeSql("select * from workflow_base a where not exists (select 1 from Workflow_VersionInfo where a.id = Workflow_VersionInfo.wfid)" + 
                "AND NOT EXISTS (SELECT 1 FROM workflow_base b WHERE istemplate = '1' AND a.id=b.id)");
        if(recordSet.next()){
            result = resultNeedFix;
            Map resultMap = new HashMap<String,String>();
            resultMap.put("showMessage","流程版本信息表数据缺失，请一键修复。");
            resultList.add(resultMap);
        }else{
            resultList.add("流程版本信息表数据正常!");
        }
        (new BaseBean()).writeLog("MonitorInfoCheck  end");
        return formatReturnJsonCheck(result, resultList,this);
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {

        String setsql = "";
        String selectSql = "";
        String selectSql1 = "";

       RecordSetTrans rst=new RecordSetTrans();
       rst.setAutoCommit(false);
       try {
        recordSet.executeSql(" drop index ix_wf_monitor_info_jji ");
        rst.executeSql("delete from Workflow_VersionInfo");//先删后加
        //Statement stmt =conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY); 
        selectSql = " SELECT id FROM workflow_base a WHERE  ACTiveversionid is null  "+
                    " AND NOT EXISTS (SELECT 1 FROM workflow_base b WHERE istemplate = '1' AND a.id=b.id) ";
        RecordSet rsSel = new RecordSet();
        rsSel.executeSql(selectSql);
        String wfid = "";
        String wfversionid = "";
        while(rsSel.next()) {
            wfid = rsSel.getString("id");
            wfversionid = wfid;
            rst.executeUpdate(" insert into Workflow_VersionInfo(wfid,wfversionid) values("+wfid+","+wfversionid+")");
        }
        
        selectSql1 = " SELECT activeversionid,count(activeversionid) FROM WORKFLOW_BASE a "+
                    " WHERE activeversionid is NOT null  "+
                    " AND NOT EXISTS (SELECT 1 FROM workflow_base b WHERE istemplate = '1' AND a.id=b.id) "+
                    " GROUP BY activeversionid HAVING count(activeversionid)>0 "+
                    " ORDER BY activeversionid ";
        rsSel.executeSql(selectSql1);
        String activeversionid = "";
        List<String> wfidlist = new ArrayList<String>();
        List<String> wfversionidlist = new ArrayList<String>();
        RecordSet rsSel1 = new RecordSet();
        while(rsSel.next()) {
            activeversionid = rsSel.getString("activeversionid");
            rsSel1.executeSql(" select id from workflow_base where activeversionid = "+activeversionid);
            while(rsSel1.next()) {
                wfid = rsSel1.getString("id");
                wfversionid = wfid;
                wfidlist.add(wfid);
                wfversionidlist.add(wfversionid);
            }
            for(int i =0;i<wfidlist.size();i++){
                for(int j =0;j<wfidlist.size();j++){
                    rst.executeUpdate(" insert into Workflow_VersionInfo(wfid,wfversionid) values("+wfidlist.get(i)+","+wfversionidlist.get(j)+")");
                }
            }
            wfidlist.clear();
            wfversionidlist.clear();
        }
        rst.commit();
       }catch (Exception e) {
        e.printStackTrace();
        rst.rollback();
       }
        needReload = "1";
        return checkData(dataMap);
    }

    @Override
    public String getClassName() {
        return "流程监控数据检测";
    }

    @Override
    public String getClassDesc() {
        return "流程监控数据检测(流程版本信息表数据以及workflow_monitor_info表索引检测";
    }

}



